import { defineStore } from "pinia";
import { ref } from "vue";
import { login as loginApi } from "@/api/studentApi";
import type { ILogin } from "@/api/userApi";

export const useUserStore = defineStore("user", () => {
  const userInfo = ref({
    // account: "",
    // classId: 0,
    // id: 0,
    // nickname: "",
    // password: "123",
  });

  async function login(data: ILogin) {
    const result = await loginApi(data);
    if (result.code === 2000) {
      localStorage.setItem("user", JSON.stringify(result.data));
      userInfo.value = {
        ...result.data,
      };
    }
    return result;
  }

  async function logout() {
    localStorage.removeItem("user");
  }

  async function setInfo(data: ILogin) {
    userInfo.value = {
      ...data,
    };
  }

  return { login, logout, setInfo, userInfo };
});
